package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.common.Result;
import com.example.entity.Applications;
import com.example.entity.ConsultationRecord;
import com.example.entity.Lawyer;
import com.example.entity.User;
import com.example.exception.CustomException;
import com.example.service.ApplicationsService;
import com.example.service.ConsultationRecordService;
import com.example.service.LawyerService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/api/statistics")
public class StatisticController {
    @Resource
    private ApplicationsService applicationsService;

    @Resource
    private LawyerService lawyerService;

    @Resource
    private ConsultationRecordService consultationRecordService;

   @Resource
    private HttpServletRequest request;

    public User getUser() {
        User user = (User) request.getSession().getAttribute("user");
        if (user == null) {
            throw new CustomException("-1", "请登录");
        }
        return user;
    }

    /**
     * 统计接口
     * @return
     */
    @GetMapping
    public Result<?> statistics(){
        HashMap<String,Object> map = new HashMap<>();
        List<Lawyer> lawyerList = lawyerService.list();
        //律师总数
        map.put("lawyerCount",lawyerList.size());

        //女律师
        long female = lawyerList.stream()
                .filter(app -> "女".equals(app.getGender()))
                .count(); // 对属性值求和
        map.put("female",female);
        int male = lawyerList.size() - (int)female;
        map.put("male",male);

        List<Applications> list = applicationsService.list();

        long count1 = list.stream()
                .filter(app -> "待审核".equals(app.getStatus()))
                .count(); // 对属性值求和
        map.put("count1",count1);

        int count2 = list.size() - (int)count1;
        map.put("count2",count2);

        LambdaQueryWrapper<ConsultationRecord> query = Wrappers.<ConsultationRecord>lambdaQuery().orderByAsc(ConsultationRecord::getId);
        //如果用户身份为律师，则查询律师的聊天，否则查用户的聊天
        if (StringUtils.isNotEmpty(getUser().getLawyerUuid())) {
            query.eq(ConsultationRecord::getLawyerId,getUser().getLawyerUuid());
        }else {
            query.eq(ConsultationRecord::getUserId,getUser().getId());
        }
        List<ConsultationRecord> recordList = consultationRecordService.list(query);
        map.put("count3",recordList.size());
        return Result.success(map);
    }

}
